package com.yituo.mjworld.mvc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.core.web.core.Result;
import com.yituo.mjworld.mvc.entity.web.Product;
import com.yituo.mjworld.mvc.entity.web.WebImgArr;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;

@Service
public class ImageArrayServiceImpl extends BaseService {

  @Autowired
  QiniuServiceImpl qiniuService;

  /**
   * 获取图片数组
   *
   * @param imageArrId 图片数组ID
   * @return
   */
  public WebImgArr getImageArray(int imageArrId) {
    return (WebImgArr) this.baseDao.get(WebImgArr.class, imageArrId);
  }

  @Override
  public JSONObject getList(QueryForm form) {
    return null;
  }

  @Override
  public Result delete(DeleteForm form) {
    return null;
  }

  /**
   * 删除指定的图片
   *
   * @param id    欲操作的图片数组
   * @param index 欲删除的图片索引
   * @return
   */
  public Result deleteTheImage(int id, int index) {
    Result result = new Result();
    WebImgArr webImgArr = (WebImgArr) this.baseDao.get(WebImgArr.class, id);
    if (webImgArr == null) {
      return result.setResult(false).setMsg("图片数组ID不存在!");
    }
    String imgUrl;
    switch (index) {
      case 1:
        imgUrl = webImgArr.getImgUrl1();
        webImgArr.setImgUrl1("");
        break;
      case 2:
        imgUrl = webImgArr.getImgUrl2();
        webImgArr.setImgUrl2("");
        break;
      case 3:
        imgUrl = webImgArr.getImgUrl3();
        webImgArr.setImgUrl3("");
        break;
      case 4:
        imgUrl = webImgArr.getImgUrl4();
        webImgArr.setImgUrl4("");
        break;
      case 5:
        imgUrl = webImgArr.getImgUrl5();
        webImgArr.setImgUrl5("");
        break;
      default:
        imgUrl = null;
        break;
    }
    this.baseDao.saveOrUpdate(webImgArr);
    // 图片本身就不存在,直接返回真
    if (StringUtils.isEmpty(imgUrl)) {
      return result.setResult(true);
    }
    // 删除七牛云上的图片
    qiniuService.delete(imgUrl);
    return result.setResult(true);
  }

  /**
   * 保存或者更新
   *
   * @param baseForm 更新表单
   * @return
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   */
  @Override
  public Result saveOrUpdate(BaseForm baseForm) throws InvocationTargetException, IllegalAccessException {
    Result result = new Result();
    WebImgArr imageArrForm = (WebImgArr) baseForm;
    Product product = (Product) this.baseDao.get(Product.class, imageArrForm.getProductId());
    WebImgArr imageArrDb = (WebImgArr) this.baseDao.get(WebImgArr.class, imageArrForm.getId());
    if (product == null) {
      return result.setResult(true).setMsg("商品ID不存在");
    }
    if (imageArrDb == null) {
      // 新增
      Serializable id = this.baseDao.save(imageArrForm);
      product.setImgArrId(Integer.parseInt(id.toString()));
      this.baseDao.merge(product);
    } else {
      // 更新
      this.baseDao.merge(imageArrForm);
      product.setImgArrId(imageArrForm.getId());
      this.baseDao.merge(product);
    }
    return new Result().setResult(true);
  }
}
